package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import utils.DbUtil;
import entity.ChallengeInfo;

public class Palydaoimpl implements Palydao {

	private PreparedStatement stmt;
	private ResultSet rs;

	/*
	 * 0、加载所有发布的挑战信息 ，2、加载正在挑战信息 ，3、加载挑战结束信息 	 
	 * @see dao.Palydao#find()
	 */
	public List<ChallengeInfo> find(int status,int pagesize,int pagenum) throws Exception {
		
		String sqlstr = " select top "+pagesize+" * from VSPLAY where status = "+ status +" and id not in (select top "+pagesize*(pagenum-1)+" id from VSPLAY order by id) order by id ";
		
		List<ChallengeInfo> list = new ArrayList<ChallengeInfo>();

		Connection conn = DbUtil.getConnection();

		stmt = conn.prepareStatement(sqlstr);
		
//		stmt.setInt(1, pagesize);
//		stmt.setInt(2, status);
//		stmt.setInt(3, pagesize*(pagenum-1));
		
		rs = stmt.executeQuery();

		while (rs.next()) {
			ChallengeInfo paly = new ChallengeInfo();
			paly.setAccname(rs.getString("accept"));

			paly.setChellname(rs.getString("challenger"));

			paly.setAcctime(rs.getDate("acctime"));

			paly.setArea(rs.getString("area"));

			paly.setChelltime(rs.getDate("chelltime"));

			paly.setImage(rs.getString("playimage"));

			paly.setMoney(rs.getInt("gamemoney"));

			paly.setPlay(rs.getString("play"));

			paly.setPlaytime(rs.getDate("playtime"));

			paly.setResult(rs.getInt("result"));

			paly.setId(rs.getInt("id"));

			list.add(paly);

		}
		DbUtil.close(conn);
		return list;
	}

	public int findAll(int status) throws Exception {
		String sqlstr = " select count(*) as totalnum from VSPLAY where status = " + status;
		Connection conn = DbUtil.getConnection();
		stmt = conn.prepareStatement(sqlstr);
		
		//stmt.setInt(1, status);
		rs = stmt.executeQuery();
		int totalnum = 0;
		while(rs.next()){
			totalnum = rs.getInt("totalnum");
		}

		DbUtil.close(conn);
		return totalnum;
	}	
	
	
	
}
